//
//  PESolisLvSettingChgTimeCell.m
//  PrivateApp
//
//  Created by Yan Chen on 2025/4/22.
//

#import "PESolisLvSettingChgTimeCell.h"
#import "ZYSwitch.h"
@interface PESolisLvSettingChgTimeCell()
@property (nonatomic, strong) ZYSwitch *sw;

@property (nonatomic, strong) QMUILabel *titleLab;

@property (nonatomic, strong) QMUITextField *leftTextField;

@property (nonatomic, strong) QMUILabel *timeLab;

@property (nonatomic ,strong) QMUILabel *timeTitleLab;

@property (nonatomic, strong) QMUILabel *stateLab;

@property (nonatomic, strong) QMUILabel *socLab;

@property (nonatomic, strong) QMUILabel *powerTitleLab;

@property (nonatomic, strong) QMUILabel *powerLab;
@end
@implementation PESolisLvSettingChgTimeCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}


-(void)setModel:(RHVppSetCommonModel *)model
{
    _model = model;
    self.titleLab.text = model.rowTitle;
    RHVppSetCommonModel *swModel = [model.items safeObjectAtIndex:0];
    self.sw.on = [swModel.content boolValue];
    
    RHVppSetCommonModel *beginModel = [model.items safeObjectAtIndex:1];
    RHVppSetCommonModel *endModel = [model.items safeObjectAtIndex:2];
    self.timeTitleLab.text = beginModel.comment.rh_localized;
    self.timeLab.text = [NSString stringWithFormat:@"%@-%@",beginModel.content,endModel.content];
  
    //截止soc
    RHVppSetCommonModel *socModel =  [model.items safeObjectAtIndex:3];
    self.socLab.text = [NSString stringWithFormat:@" %@%%",socModel.content];
    
    //充放电功率
    RHVppSetCommonModel *powerModel =  [model.items safeObjectAtIndex:4];
    self.powerLab.text = [NSString stringWithFormat:@" %@A",powerModel.content];
    self.powerTitleLab.text = powerModel.comment.rh_localized;
    //还原model原始倍率值  233的不处理精度问题
    if(powerModel.content.length > 0){
        NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:powerModel.content];
        self.powerLab.text = [NSString stringWithFormat:@"%@A",number];
        if(powerModel.ratio.length > 0){//233的不处理精度问题
            NSDecimalNumber *step = [NSDecimalNumber decimalNumberWithString:powerModel.ratio];
            NSDecimalNumber *result = [number decimalNumberByMultiplyingBy:step];
            self.powerLab.text = [NSString stringWithFormat:@"%@A",result];
        }
    }
   

   
//    self.timeLab.text = [NSString stringWithFormat:@"%ld--%ld",beginModel.regAddr,endModel.regAddr];
//    self.powerLab.text = [NSString stringWithFormat:@"%ld",powerModel.regAddr];
//    self.socLab.text = [NSString stringWithFormat:@"%ld",socModel.regAddr];
//    self.timeTitleLab.text = [NSString stringWithFormat:@"%@ %ld %ld %ld",self.timeTitleLab.text,swModel.regAddr,swModel.num,swModel.offset];
}

-(void)setupUI
{
    kWeakSelf;
    self.contentView.backgroundColor = kCOLOR_HEX(@"#F0F5FF");
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = UIColor.whiteColor;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 15, 0, 15));
    }];
    
    QMUILabel *titleLab = [QMUILabel labelWithText:@"" font:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6)];
    [bgView addSubview:titleLab];
    self.titleLab = titleLab;
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(10);
        make.left.mas_offset(15);
    }];
    
    
    _sw = [[ZYSwitch alloc]initWithFrame:CGRectMake(0, 0, 45, 25)];
    [bgView addSubview:_sw];
    [_sw mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(45);
        make.height.offset(25);
        make.centerY.equalTo(weakSelf.titleLab);
        make.right.mas_offset(-15);
    }];
    [_sw addTarget:self action:@selector(swicthValueDidChange:) forControlEvents:UIControlEventValueChanged];
   
    
    UIView *bottom_bgView = kView;
    bottom_bgView.backgroundColor = kCOLOR_HEX(@"#F3F7FF");
    bottom_bgView.layer.cornerRadius = 8;
    bottom_bgView.layer.masksToBounds = YES;
    [bgView addSubview:bottom_bgView];
    [bottom_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.equalTo(weakSelf.titleLab.mas_bottom).offset(10);
        make.bottom.mas_offset(-15);
    }];
    
    
    
    _timeTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6) textAligment:NSTextAlignmentLeft];
    [bottom_bgView addSubview:_timeTitleLab];
    [_timeTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(24);
        make.top.mas_offset(15);
        make.left.mas_offset(15);
    }];
    
    UIImageView *arrow1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow_2"]];
    [bottom_bgView addSubview:arrow1];
    [arrow1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(18);
        make.centerY.equalTo(weakSelf.timeTitleLab);
        make.right.mas_offset(-15);
    }];
    
    _timeLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6) textAligment:NSTextAlignmentRight];
    [bottom_bgView addSubview:_timeLab];
    [_timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.timeTitleLab.mas_right).offset(5);
        make.right.equalTo(arrow1.mas_left).offset(-5);
        make.centerY.equalTo(weakSelf.timeTitleLab);
    }];
    
    _powerTitleLab = [QMUILabel labelWithText:[NSString stringWithFormat:@"%@:",@"充电电流".rh_localized] font:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6)];
    _powerTitleLab.textAlignment = NSTextAlignmentLeft;
    [bottom_bgView addSubview:_powerTitleLab];
    [_powerTitleLab sizeToFit];
    [_powerTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.timeTitleLab);
        make.width.offset(weakSelf.powerTitleLab.width+1);
        make.top.equalTo(weakSelf.timeTitleLab.mas_bottom).offset(15);
       
    }];
    _powerLab = [QMUILabel labelWithText:@"--" font:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6)];
    _powerLab.textAlignment = NSTextAlignmentRight;
    [bottom_bgView addSubview:_powerLab];
    [_powerLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.powerTitleLab.mas_right);
        make.right.mas_offset(-15);
        make.centerY.equalTo(weakSelf.powerTitleLab);
    }];
    
    
    QMUILabel *socTitleLab = [QMUILabel labelWithText:@"SOC:" font:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6)];
    socTitleLab.textAlignment = NSTextAlignmentLeft;
    [bottom_bgView addSubview:socTitleLab];
    [socTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.timeTitleLab);
        make.width.offset(50);
        make.top.equalTo(weakSelf.powerTitleLab.mas_bottom).offset(15);
        make.bottom.mas_offset(-15);
    }];
    
    _socLab = [QMUILabel labelWithText:@"--" font:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6)];
    _socLab.textAlignment = NSTextAlignmentRight;
    [bottom_bgView addSubview:_socLab];
    [_socLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(socTitleLab.mas_right);
        make.right.equalTo(weakSelf.powerLab);
        make.centerY.equalTo(socTitleLab);
    }];
    
   
    
    
    
    
    QMUIButton *leftBtn = [[QMUIButton alloc]init];
    [bgView addSubview:leftBtn];
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(bottom_bgView);
    }];
    

    [[leftBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (weakSelf.selectAction) {
            weakSelf.selectAction(weakSelf.model);
        }
    }];
    
}

-(void)swicthValueDidChange:(ZYSwitch*)sw
{
    RHVppSetCommonModel *swModel = [self.model.items safeObjectAtIndex:0];
    swModel.content = [NSString stringWithFormat:@"%d",sw.isOn];
}
@end
